using System;
using System.Collections.Generic;
using System.Linq;
using Medianamik.Core.Urls;

namespace Medianamik.IntegrationTests.Medianamik.Core.Urls
{
    public class UrlDictionaryFake : UrlDictionary
    {
        protected override IEnumerable<IUrlDictionaryEntry> GetUrlDictionaryEntries()
        {
            return Entries;
        }

        protected override IEnumerable<IUrlDictionaryEntry> GetUrlDictionayEntries(Guid nodeId)
        {
            return _entriesByNodeId[nodeId];
        }

        protected override IEnumerable<IUrlDictionaryEntry> GetDefaultDocumentEntries()
        {
            return DefaultDocumentEntries;
        }

        private List<IUrlDictionaryEntry> _entries = new List<IUrlDictionaryEntry>();
        public List<IUrlDictionaryEntry> Entries
        {
            get { return _entries; }
            set { _entries = value; }
        }

        private List<IUrlDictionaryEntry> _defaultDocumentEntries = new List<IUrlDictionaryEntry>();
        public List<IUrlDictionaryEntry> DefaultDocumentEntries
        {
            get { return _defaultDocumentEntries; }
            set { _defaultDocumentEntries = value; }
        }

        private Dictionary<Guid, List<IUrlDictionaryEntry>> _entriesByNodeId =
            new Dictionary<Guid, List<IUrlDictionaryEntry>>();
        
        public void SetEntriesForUpdateByNodeId(IEnumerable<IUrlDictionaryEntry> entries)
        {
            var entriesToAdd = entries.ToList();

            var nodeId = entriesToAdd.First().NodeId;

            if (!entries.All(e => e.NodeId.Equals(nodeId)))
                throw new Exception("All entries should have the same NodeID.");

            _entriesByNodeId.Remove(nodeId);
            _entriesByNodeId.Add(nodeId, entriesToAdd);

        }
    }
}